package cn.mxz.controller;

import cn.mxz.pojo.ResultData;
import cn.mxz.pojo.ShoppingCart;
import cn.mxz.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 小毛
 * @Date 2021/12/4 11:09
 * @Remark 佛祖保佑永无bug~
 */
@RestController
@CrossOrigin
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //根据用户ID查询所有订单
    @GetMapping("{userId}")
    public ResultData orderAll(@PathVariable String userId,String status){
        if (status == null){
            return orderService.selectByUserId(userId);
        }else {
            return orderService.selectByStatus(userId,status);
        }
    }

    //删除订单接口
    @DeleteMapping("{orderId}")
    public ResultData deleteByOrderId(@PathVariable String orderId){
        return orderService.deleteByOrderId(orderId);
    }

    //添加订单接口
    @PutMapping("cartadd")
    public ResultData orderInsert(@RequestBody List<ShoppingCart> cartlist,String name,String mobile,String province,String city,String area,String addr,String userId){
        return orderService.insertOrder(cartlist, name, mobile, province, city, area, addr, userId);
    }

    //将对应id的订单改为”待发货“
    @PutMapping("{orderId}")
    public ResultData orderStatusChange(@PathVariable String orderId){
        return orderService.changeStatus(orderId);
    }

    //将对应id的订单改为"待收货"
    @PutMapping("/fahuo/{orderId}")
    public ResultData faHuo(@PathVariable String orderId){
        try {
            Thread.sleep(5000);
            return orderService.faHuo(orderId);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ResultData(319,"接口异常");
    }

    //将对应id的订单改为"待评价"
    @PutMapping("/shouhuo/{orderId}")
    public ResultData shouHuo(@PathVariable String orderId){
        return orderService.shouHuo(orderId);
    }
}
